package bc.controller;

import bc.model.HtmlContext;

import bc.event.StageResize;
import bc.event.LaunchStart;

import js.Lib;
import js.Dom;

import bpmjs.Event;

import haxe.rtti.Infos;

@ManagedEvents("stageResize")
class StageResizeAction extends EventDispatcher, implements Infos
{
	@Inject
	public var htmlContext : HtmlContext;

	@Complete
	public function handleComplete()
	{
		updateSize();
	}
	
	@MessageHandler
	public function handleLaunchStart(event : LaunchStart)
	{
		Lib.window.onresize = onResize;		
	}
	
	private function onResize(event : Event)
	{
		updateSize();
		fireUpdate();
	}
	
	private function updateSize()
	{
		htmlContext.windowWidth = Lib.window.innerWidth; 
		htmlContext.windowHeight = Lib.window.innerHeight;
	}
	
	private function fireUpdate()
	{
		dispatchEvent(new StageResize());
	}
}
